package com;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class Cartoon extends JFrame {

    private int[][] date = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };
    private int x0;
    private int y0;

    public Cartoon ()
    {
           initboard();
           paintboard();
           rePaintView();
           this.setVisible(true);
    }
    public void initboard()
    {
        this.setTitle("动漫拼图");
        this.setDefaultCloseOperation(3);
        this.setSize(960, 565);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
    }
    public void paintboard()
    {
        //标题`
        JLabel title = new JLabel(new ImageIcon("arr\\images\\title.png"));
        title.setBounds(354, 27, 232, 57);
        this.add(title);
        //动漫图片
        JPanel imagejpanel = new JPanel();
        imagejpanel.setBounds(150, 114, 360, 360);
        imagejpanel.setLayout(null);
        Random rand = new Random();
        for (int i = 0; i < date.length; i++) {
            for (int j = 0; j < date[i].length; j++) {
                int x = rand.nextInt(date.length);
                int y = rand.nextInt(date[i].length);
                int temp = date[i][j];
                date[i][j] = date[x][y];
                date[x][y] = temp;
            }
        }
        for (int i = 0; i < date.length; i++) {
            for (int j = 0; j < date[i].length; j++) {
                JLabel jl = new JLabel(new ImageIcon("arr\\images\\" + date[i][j] + ".png"));
                jl.setBounds(90 * j, 90 * i, 90, 90);
                imagejpanel.add(jl);
            }
        }
        wc:for (int i = 0; i < date.length; i++) {
            for (int j = 0; j < date[i].length; j++) {
                if(date[i][j] == 0)
                {
                    x0=i;
                    y0=j;
                    break wc;
                }
            }
        }
        this.add(imagejpanel);
        //参照图
        JLabel picture = new JLabel(new ImageIcon("arr\\images\\canzhaotu.png"));
        picture.setBounds(574, 114, 122, 121);
        this.add(picture);
        //按钮
        JButton on = new JButton(new ImageIcon("arr\\images\\shang.png"));
        on.setBounds(732, 265, 57, 57);
        on.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(x0==0)
                {
                    return;
                }
                date[x0][y0]=date[x0-1][y0];
                date[x0-1][y0]=0;
                x0=x0-1;
                rePaintView();
            }
        });
        this.add(on);
        JButton left = new JButton(new ImageIcon("arr\\images\\zuo.png"));
        left.setBounds(650, 347, 57, 57);
        left.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(y0==0)
                {
                    return;
                }
                date[x0][y0]=date[x0][y0-1];
                date[x0][y0-1]=0;
                y0=y0-1;
                rePaintView();
            }
        });
        this.add(left);

        JButton buttom = new JButton(new ImageIcon("arr\\images\\xia.png"));
        buttom.setBounds(732, 347, 57, 57);
        buttom.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(x0==3)
                {
                    return;
                }
                date[x0][y0]=date[x0+1][y0];
                date[x0+1][y0]=0;
                x0=x0+1;
                rePaintView();
            }
        });
        this.add(buttom);
        JButton right = new JButton(new ImageIcon("arr\\images\\you.png"));
        right.setBounds(813, 347, 57, 57);
        right.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(y0==3)
                {
                    return;
                }
                date[x0][y0]=date[x0][y0+1];
                date[x0][y0+1]=0;
                y0=y0+1;
                rePaintView();
            }
        });
        issuccess();
        this.add(right);
        JButton help = new JButton(new ImageIcon("arr\\images\\qiuzhu.png"));
        help.setBounds(626, 444, 108, 45);
        help.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                   int[][] dates = {
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                        {13, 14, 15, 0}
                };date=dates;
                x0=3;
                y0=3;
                rePaintView();
            }
        });
        this.add(help);
        JButton reset = new JButton(new ImageIcon("arr\\images\\chongzhi.png"));
        reset.setBounds(786, 444, 108, 45);
        reset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Random rand = new Random();
                for (int i = 0; i < date.length; i++) {
                    for (int j = 0; j < date[i].length; j++) {
                        int x = rand.nextInt(date.length);
                        int y = rand.nextInt(date[i].length);
                        int temp = date[i][j];
                        date[i][j] = date[x][y];
                        date[x][y] = temp;
                    }
                }
                wc:for (int i = 0; i < date.length; i++) {
                    for (int j = 0; j < date[i].length; j++) {
                        if(date[i][j] == 0)
                        {
                            x0=i;
                            y0=j;
                            break wc;
                        }
                    }
                }
                rePaintView();
            }
        });
        this.add(reset);

        //背景
        JLabel image = new JLabel(new ImageIcon("arr\\images\\background.png"));
        image.setBounds(0, 0, 960, 565);
        this.add(image);
    }
    public void rePaintView( )
    {
        JPanel imagejpanel = new JPanel();
        imagejpanel.setBounds(150, 114, 360, 360);
        imagejpanel.setLayout(null);
        imagejpanel.removeAll();
        for (int i = 0; i < date.length; i++) {
            for (int j = 0; j < date[i].length; j++) {
                JLabel jl = new JLabel(new ImageIcon("arr\\images\\" + date[i][j] + ".png"));
                jl.setBounds(90 * j, 90 * i, 90, 90);
                imagejpanel.add(jl);
            }
        }
        this.add(imagejpanel);
        imagejpanel.repaint();
    }
    public boolean success()
    {
        int[][]dates= {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 0}
        };
        for (int i = 0; i < date.length; i++) {
            for (int j = 0; j < date[i].length; j++) {
                if(date[i][j]!=dates[i][j])
                {
                    return false;
                }
            }
        }
        return true;
    }
    public void issuccess()
    {
        if(success()==true)
        {
            JOptionPane.showMessageDialog(this,"拼图成功");
        }
    }

}
